from rest_framework import serializers
from question.models import Label, Question, Reply
from headlines.models import Comment
from user.models import User


class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = [
            'id',
            'label_name'
        ]


class QuestionSimpleSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()

    class Meta:
        model = Question
        fields = "__all__"


class QuestionSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()
    labels = serializers.StringRelatedField(many=True)

    class Meta:
        model = Question
        fields = "__all__"


class UserSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'avatar']


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username']


class SubReplySerializer(serializers.ModelSerializer):
    user = UserSimpleSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'unuseful_count',
            'user'
        ]


class ReplySerializer(serializers.ModelSerializer):
    subs = SubReplySerializer(read_only=True, many=True)
    user = UserSerializer()

    class Meta:
        model = Reply
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'problem',
            'unuseful_count',
            'subs',
            'user',
            'parent'
        ]


class CommentSerializer(serializers.ModelSerializer):
    subs = SubReplySerializer(read_only=True, many=True)
    user = UserSerializer(read_only=True)

    class Meta:
        model = Comment
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'problem',
            'unuseful_count',
            'subs',
            'user',
            'parent'
        ]


class QuestionInfoSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(many=True, read_only=True)
    replies = ReplySerializer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = [
            'id',
            'createtime',
            'labels',
            'reply',
            'replyname',
            'replytime',
            'title',
            'unuseful_count',
            'useful_count',
            'user',
            'visits',
            'content',
            'replies'
        ]

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['comment_question'] = []
        data['answer_question'] = data.pop('replies')
        return data


"""
+1
"""


class ReplySimpleSerializer0(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()

    class Meta:
        model = Reply
        fields = [
            'problem',
            'content',
            'type',
            'user',
            'user_id'
        ]

class QuestionSerializer0(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Question
        fields = [
            'id',
            'createtime',
            'labels',
            'reply',
            'replyname',
            'replytime',
            'title',
            'unuseful_count',
            'useful_count',
            'user',
            'visits'
        ]

class ReplySimpleSerializer0(serializers.ModelSerializer):
    problem = serializers.IntegerField()
    parent = serializers.IntegerField()
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()

    class Meta:
        model = Reply
        fields = [
            'problem',
            'content',
            'type',
            'parent',
            'user',
        ]
class LabelInfoSerializer(serializers.ModelSerializer):
    questions = QuestionSerializer0(read_only=True, many=True)

    # users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    class Meta:
        model = Label
        fields = [
            'id',
            'questions',
            'label_name',
            'desc',
            'baike_url',
            'label_icon',
            'users',
        ]
